/**
 * 	题型为需要多次答题的题型
 */
export const times = {
	data() {
		return {
			isSureList: [],
			current: 0,
			isArticleShow: true,
			isTimeEnd: false
		}
	},
	methods: {
		/**
		 * 	播放时开启定时器
		 */
		questionPlay: function() {
			this.startTimer()
			this.play()
		},
		startTimer: function() {
			const timer = setTimeout(() => {
				this.isTimeEnd = true
			}, this.assignment.answerLimitMs)
			this.$once('hook:beforeDestory', function() {
				clearTimeout(timer)
			})
		},
		tap: function(item, index) {
			this.touchAudio()
			const count = this.assignment.optionArticles[this.current].filter(item => {
					return item.isMatched
				})
				.length
			if (this.choosesKey.length === count) return
			this.questionPause()
			this.optionStatus('select', index)
			this.choosesKey.push(item.isMatched)
			if (this.choosesKey.length === count) {
				const isSure = this.choosesKey.every(item => {
					return item
				})
				this.isSureList.push(isSure)
				const timer = setTimeout(() => {
					this.next()
				}, 1500)
				this.$once('hook:beforeDestory', function() {
					clearTimeout(timer)
				})
			}
		},
		next: function() {
			if (this.isTimeEnd || this.current === this.assignment.optionArticles.length - 1) {
				const isSure = this.isSureList.every(item => {
					return item
				})
				this.$parent.next({
					isSure: isSure
				})
				return
			}
			this.current++
			this.chooses = []
			this.choosesKey = []
			this.play()
			this.isArticleShow = false
			setTimeout(() => {
				this.isArticleShow = true
			}, 0)
		},
		/**
		 *  点击icon 
		 * 	播放/暂停
		 */
		iconTap: function() {
			this.touchAudio()
			this.questionPress ? this.questionPause() : this.questionPlay()
		},
		/**
		 * 	播放问题
		 */
		play: async function() {
			const data = await this.parseTextAudio({
				text: this.assignment.questionName,
				speaker: this.assignment.audioSpeaker ? this.assignment.audioSpeaker : null
			})
			this.dialogueAudio({
				src: data,
				start: () => {
					this.questionPress = true
				},
				end: () => {
					this.questionPress = false
				}
			})
		},
		questionPause: function() {
			this.dialogueAudio({
				play: false
			})
			this.questionPress = false
		}
	}
}
